package com.tudouji.project.bill.controller;

import com.tudouji.common.utils.poi.ExcelTempUtil;
import com.tudouji.common.utils.poi.ExcelUtil;
import com.tudouji.framework.aspectj.lang.annotation.Log;
import com.tudouji.framework.aspectj.lang.enums.BusinessType;
import com.tudouji.framework.interceptor.annotation.RepeatSubmit;
import com.tudouji.framework.web.controller.BaseController;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.framework.web.page.TableDataInfo;
import com.tudouji.project.bill.domain.Transferapply;
import com.tudouji.project.bill.mapper.TransferapplyMapper;
import com.tudouji.project.bill.service.ITransferapplyService;
import com.tudouji.project.wlinfo.domain.Wlinfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 库房调拨Controller
 *
 * @author tudouji
 * @date 2021-03-22
 */
@Api(tags = {"调拨"})
@RestController
@RequestMapping("/bill/transferapply")
public class TransferapplyController extends BaseController {
    @Autowired
    private ITransferapplyService transferapplyService;
    @Autowired
    private TransferapplyMapper transferapplyMapper;
    @Autowired
    private ExcelTempUtil excelTempUtil;

    /**
     * 查询库房调拨列表:实体里必需要传参flowid
     */
    @ApiOperation(value = "微信端调拨列表", httpMethod = "GET", notes = "微信端调拨列表")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "transferapply", value = "实体", required = false, dataType = "transferapply", paramType = "query"),
                    @ApiImplicitParam(name = "status", value = "状态", required = false, dataType = "String", paramType = "query"),
                    @ApiImplicitParam(name = "functionType", value = "权限", required = true, dataType = "String", paramType = "query")
            }
    )
    @GetMapping("/wxlist")
    public TableDataInfo wxlist(Transferapply transferapply, String status, String functionType) {
        List<Transferapply> list = transferapplyService.selectTransferapplyList(transferapply, status, functionType);
        return getDataTable(list);
    }

    /**
     * 查询库房调拨列表
     */
    @PreAuthorize("@ss.hasPermi('bill:transferapply:list')")
    @GetMapping("/list")
    public TableDataInfo list(Transferapply transferapply, @RequestParam(required = false) String type
            , @RequestParam(required = false) String functiontype, @RequestParam(required = false) String status
            , @RequestParam(required = false) String search) {
        startPage();
        if (StringUtils.isNotBlank(status)) {
            String[] sts = status.split(",");
            Integer[] stats = (Integer[]) ConvertUtils.convert(sts, Integer.class);
            transferapply.setStats(stats);
        }else{
            transferapply.setNostats(new Integer[]{0, -1});
        }
        if (StringUtils.isNotBlank(type)) {
            String[] sts = type.split(",");
            Integer[] types = (Integer[]) ConvertUtils.convert(sts, Integer.class);
            transferapply.setTypes(types);
        }
        if (StringUtils.isNotBlank(search)) {
            transferapply.setSearch(search);
        }
        List<Transferapply> list = transferapplyService.findPagePC(transferapply, functiontype);
        if (list.size() > 0){
            for (int i = 0; i < list.size(); i++) {
                Transferapply item = list.get(i);
                if (item.getType() == 3){
                    item.setFromsrname(item.getTosrname());
                    item.setOrgname(item.getToorgname());
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出库房调拨列表
     */
    @PreAuthorize("@ss.hasPermi('bill:transferapply:export')")
    @Log(title = "库房调拨", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Transferapply transferapply, @RequestParam(required = false) String type
            , @RequestParam(required = false) String functiontype, @RequestParam(required = false) String status
            , @RequestParam(required = false) String search) {
        if (StringUtils.isNotBlank(status)) {
            String[] sts = status.split(",");
            Integer[] stats = (Integer[]) ConvertUtils.convert(sts, Integer.class);
            transferapply.setStats(stats);
        }else{
            transferapply.setNostats(new Integer[]{0, -1});
        }
        if (StringUtils.isNotBlank(type)) {
            String[] sts = type.split(",");
            Integer[] types = (Integer[]) ConvertUtils.convert(sts, Integer.class);
            transferapply.setTypes(types);
        }
        if (StringUtils.isNotBlank(search)) {
            transferapply.setSearch(search);
        }
        List<Transferapply> list = transferapplyService.findPagePC(transferapply, null);
//        ExcelUtil<Transferapply> util = new ExcelUtil<Transferapply>(Transferapply.class);
//        return util.exportExcel(list, "transferapply");
        if (StringUtils.isBlank(transferapply.getFlowid())){
            return excelTempUtil.exportCommon(list, "transferapply");
        }
        return excelTempUtil.exportCommon(list, StringUtils.equalsIgnoreCase(transferapply.getFlowid(), "27") ? "其他出入库" : "库房调拨单");
    }

//    /**
//     * 获取库房调拨详细信息
//     */
//    @ApiOperation(value = "调拨详情", httpMethod = "GET", notes = "调拨详情")
//    @ApiImplicitParams(
//            {
//                    @ApiImplicitParam(name = "guid", value = "guid", required = true, dataType = "String", paramType = "path")
//            }
//    )
//    @GetMapping(value = "/{guid}")
//    public AjaxResult getInfo(@PathVariable("guid") String guid) {
//        return AjaxResult.success(transferapplyService.selectTransferapplyById(guid));
//    }

    @ApiOperation(value = "调拨详情", httpMethod = "GET", notes = "调拨详情")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfoById(@PathVariable("id") Long id) {
        return transferapplyService.findDetail(id);
    }

    /**
     * 新增库房调拨
     */
    @PreAuthorize("@ss.hasPermi('bill:transferapply:add')")
    @Log(title = "库房调拨", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Transferapply transferapply) {
        return toAjax(transferapplyService.insertTransferapply(transferapply));
    }

    /**
     * 修改库房调拨
     */
    @PreAuthorize("@ss.hasPermi('bill:transferapply:edit')")
    @Log(title = "库房调拨", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Transferapply transferapply) {
        return toAjax(transferapplyService.updateTransferapply(transferapply));
    }

    /**
     * 删除库房调拨
     */
    @PreAuthorize("@ss.hasPermi('bill:transferapply:remove')")
    @Log(title = "库房调拨", businessType = BusinessType.DELETE)
    @DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids) {
        return toAjax(transferapplyService.deleteTransferapplyByIds(guids));
    }

    /**
     * 发起
     */
    @RepeatSubmit
    @ApiOperation(value = "发起", httpMethod = "POST", notes = "发起")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "transferapply", value = "实体", required = true, dataType = "String", paramType = "body"),
                    @ApiImplicitParam(name = "fileIds", value = "附件id", required = false, dataType = "String", paramType = "query")
            }
    )
    @Log(title = "发起", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody Transferapply entity, String fileIds) {
        try {
            return transferapplyService.save(entity, fileIds);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @RepeatSubmit
    @ApiOperation(value = "审批", httpMethod = "POST", notes = "审批")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "taskid", value = "taskid", required = true, dataType = "Long", paramType = "query"),
                    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "query"),
                    @ApiImplicitParam(name = "opinion", value = "审批意见", required = false, dataType = "String", paramType = "query"),
                    @ApiImplicitParam(name = "fileIds", value = "附件id", required = false, dataType = "String", paramType = "query")
            }
    )
    @Log(title = "审批", businessType = BusinessType.OTHER)
    @PostMapping("/approval")
    public AjaxResult approval(Long taskid, Long id, String opinion, String fileIds) {
        return transferapplyService.approval(id, fileIds, opinion, taskid);
    }

    @RepeatSubmit
    @ApiOperation(value = "内部价格确认", httpMethod = "POST", notes = "内部价格确认")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "taskid", value = "taskid", required = true, dataType = "Long", paramType = "query"),
                    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "query"),
                    @ApiImplicitParam(name = "opinion", value = "审批意见", required = false, dataType = "String", paramType = "query"),
                    @ApiImplicitParam(name = "fileIds", value = "附件id", required = false, dataType = "String", paramType = "query"),
                    @ApiImplicitParam(name = "items", value = "明细", required = true, dataType = "List", paramType = "body")
            }
    )
    @Log(title = "内部价格确认", businessType = BusinessType.OTHER)
    @PostMapping("/finance")
    public AjaxResult finance(Long taskid, Long id, String opinion, @RequestBody Transferapply entity, String fileIds) {
        return transferapplyService.finance(id, fileIds, opinion, entity.getItems(), taskid);
    }

    @RepeatSubmit
    @ApiOperation(value = "入库", httpMethod = "POST", notes = "入库")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "taskid", value = "taskid", required = true, dataType = "Long", paramType = "query"),
                    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "query"),
                    @ApiImplicitParam(name = "opinion", value = "审批意见", required = false, dataType = "String", paramType = "query"),
                    @ApiImplicitParam(name = "fileIds", value = "附件id", required = false, dataType = "String", paramType = "query"),
                    @ApiImplicitParam(name = "items", value = "明细", required = true, dataType = "List", paramType = "body")
            }
    )
    @Log(title = "入库", businessType = BusinessType.OTHER)
    @PostMapping("/transferWhin")
    public AjaxResult transferWhin(Long taskid, @RequestBody  Transferapply entity, Long id, String opinion, String fileIds) {
        return transferapplyService.transferWhin(id, entity.getItemsIn(), fileIds, opinion, taskid, null);
    }

    @RepeatSubmit
    @ApiOperation(value = "出库", httpMethod = "POST", notes = "出库")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "taskid", value = "taskid", required = true, dataType = "Long", paramType = "query"),
                    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "query"),
                    @ApiImplicitParam(name = "opinion", value = "审批意见", required = false, dataType = "String", paramType = "query"),
                    @ApiImplicitParam(name = "fileIds", value = "附件id", required = false, dataType = "String", paramType = "query"),
                    @ApiImplicitParam(name = "items", value = "明细", required = true, dataType = "List", paramType = "body")
            }
    )
    //itemsOut传: itemguid:业务明细id
    @Log(title = "出库", businessType = BusinessType.OTHER)
    @PostMapping("/tranferWhout")
    public AjaxResult tranferWhout(Long taskid, @RequestBody Transferapply entity, Long id, String opinion, String fileIds) {
        return transferapplyService.tranferWhout(id, entity.getItemsOut(), fileIds, opinion, taskid, null);
    }

    @RepeatSubmit
    @ApiOperation(value = "反出库", httpMethod = "POST", notes = "出库")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "taskid", value = "taskid", required = true, dataType = "Long", paramType = "query"),
                    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "query"),
                    @ApiImplicitParam(name = "opinion", value = "审批意见", required = false, dataType = "String", paramType = "query"),
                    @ApiImplicitParam(name = "fileIds", value = "附件id", required = false, dataType = "String", paramType = "query"),
            }
    )
    @Log(title = "反出库", businessType = BusinessType.OTHER)
    @PostMapping("/whoutBack")
    public AjaxResult whoutBack(Long taskid, Long id, String opinion, String fileIds) {
        return transferapplyService.whoutBack(id, fileIds, opinion, taskid);
    }

    @RepeatSubmit
    @ApiOperation(value = "调整确认（财务审批）", httpMethod = "POST", notes = "出库")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "taskid", value = "taskid", required = true, dataType = "Long", paramType = "query"),
                    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "query"),
                    @ApiImplicitParam(name = "opinion", value = "审批意见", required = false, dataType = "String", paramType = "query"),
                    @ApiImplicitParam(name = "fileIds", value = "附件id", required = false, dataType = "String", paramType = "query"),
                    @ApiImplicitParam(name = "quality", value = "单据性质", required = true, dataType = "Integer", paramType = "query")
            }
    )
    @Log(title = "调整确认（财务审批）", businessType = BusinessType.OTHER)
    @PostMapping("/financial")
    public AjaxResult financial(Long taskid, Long id, String opinion,String fileIds
            , Integer quality) {
        return transferapplyService.financial(id, fileIds, opinion, taskid, quality);
    }

    @RepeatSubmit
    @ApiOperation(value = "拒绝", httpMethod = "POST", notes = "拒绝")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "taskid", value = "taskid", required = true, dataType = "Long", paramType = "query"),
                    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "query"),
                    @ApiImplicitParam(name = "opinion", value = "审批意见", required = false, dataType = "String", paramType = "query"),
                    @ApiImplicitParam(name = "fileIds", value = "附件id", required = false, dataType = "String", paramType = "query"),
                    @ApiImplicitParam(name = "refuseNodeid", value = "拒绝节点", required = true, dataType = "Long", paramType = "query"),
                    @ApiImplicitParam(name = "backUserid", value = "拒绝给谁", required = true, dataType = "Long", paramType = "query")
            }
    )
    @Log(title = "拒绝", businessType = BusinessType.OTHER)
    @PostMapping("/refuse")
    public AjaxResult refuse(Long taskid, Long id, String opinion, String fileIds, Long refuseNodeid, Long backUserid) {
        return transferapplyService.refuse(id, fileIds, opinion, taskid, refuseNodeid, backUserid);
    }

    @RepeatSubmit
    @ApiOperation(value = "反入库", httpMethod = "POST", notes = "反入库")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "taskid", value = "taskid", required = true, dataType = "Long", paramType = "query"),
                    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "query"),
                    @ApiImplicitParam(name = "opinion", value = "审批意见", required = false, dataType = "String", paramType = "query"),
                    @ApiImplicitParam(name = "fileIds", value = "附件id", required = false, dataType = "String", paramType = "query")
            }
    )
    @Log(title = "反入库", businessType = BusinessType.OTHER)
    @PostMapping("/whinBack")
    public AjaxResult whinBack(Long taskid, Long id, String opinion, String fileIds) {
        return transferapplyService.whinBack(id, fileIds, opinion, taskid);
    }

    @RepeatSubmit
    @Log(title = "调拨派车", businessType = BusinessType.OTHER)
    @PostMapping("/reSendCar")
    public AjaxResult reSendCar(@RequestBody Wlinfo wlinfo, Long taskid, Long id, String opinion, String fileIds) {

        return transferapplyService.reSendCar(wlinfo,id, fileIds, opinion, taskid);
    }

    @RepeatSubmit
    @Log(title = "调拨派车物流数据", businessType = BusinessType.OTHER)
    @GetMapping("/reToSendCar")
    public AjaxResult reToSendCar(Long id) {
        return transferapplyService.reToSendCar(id);
    }

    /**
     其他出入库，出入库及之前可以删除；
     调拨派车及之前可以删除，
     */
    @RepeatSubmit
    @Log(title = "删除", businessType = BusinessType.DELETE)
    @PostMapping("/del")
    public AjaxResult del(Long id) {
        return transferapplyService.del(id);
    }
}
